//
//  HZHomeTc.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/16.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit

class HZHomeTc: HZBaseTc {
    

    //懒加载出处理业务逻辑的类
    lazy var homeVM = HZHomeVM()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
     

        if !login {
            
            //判断是否显示登录的界面
            initView.setNotLogin(nil, homeImgName:nil)
            return
        }
     
        
        
        
        //请求数据并刷新界面
        giveHome()
//        print(tableView)
        
//        预估 cell 的高度
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        
        
        //设置 cell没有分割线
        tableView.separatorStyle = .None
        
        
        //设置tableView的背景色
        tableView.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        
        //注册 cell
        tableView.registerClass(HZHomeCell.self, forCellReuseIdentifier: "homecell")
        
        
        //设置 footerView
        tableView.tableFooterView = activity
        
//        self.refreshControl = UIRefreshControl()
        
        //添加下拉刷新的控件
        tableView.addSubview(refreshCon)
        
        //设置下拉刷新控件的位置
        refreshCon.frame.origin.y = -50
        
        refreshCon.addTarget(self, action: "refreshDate", forControlEvents: .TouchUpInside)
        
        
        //添加提示的标栏
        self.navigationController!.view.insertSubview(reminderLabel, belowSubview: (self.navigationController?.navigationBar)!)
    }
    
    
    //下拉刷新的监听方法
    func refreshDate(){
        
        
//        print("嘻嘻")
        
        //获取数据
        giveHome()
        
        
        
    }
    
    
    
    
    func giveHome(){
        
        
        homeVM.giveHomeDate({ (sucee, count) -> () in
            
            if sucee == false{
                
                print("请求失败了")
                return
            }else{
                
                //全局刷新
                self.tableView.reloadData()
                
                //如果是下拉刷新
                if self.activity.isAnimating() == false {
                   
                    //出现标题
                    self.reminder(count)
                }
                
                
                //上拉加载完了之后就停止菊花并隐藏
                self.stopAm()
                
            }
            
            
            }, isPull: activity.isAnimating())
    }
    
    
    
    // MARK: -  出现下拉条目标题的方法
    private func reminder(conut:Int){
        
        //设置初始位置
        reminderLabel.frame.origin.y = CGRectGetMaxY((navigationController?.navigationBar.frame)!)-reminderLabel.frame.size.height
        
        //设置文本
        if conut == 0 {
            
            reminderLabel.text = "没有新消息"
        } else {
            
            reminderLabel.text = "\(conut)条新消息"
        }
        
        
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            //动画出现
            self.reminderLabel.hidden = false
            
            self.reminderLabel.transform = CGAffineTransformMakeTranslation(0, self.reminderLabel.frame.size.height)
            }) { (_) -> Void in
                
               UIView.animateWithDuration(1, delay: 0.5, options: [], animations: { () -> Void in
                
                 self.reminderLabel.transform = CGAffineTransformIdentity
                
                }, completion: { (_) -> Void in
                    
                    //隐藏
                    self.reminderLabel.hidden = true
                    
               })
                
        }
        
    }
    

    
    private func stopAm(){
        
        self.activity.stopAnimating()
        self.activity.hidesWhenStopped = true
        
        //恢复初始
        refreshCon.stopRef()
        
    }
    
    

    
    //懒加载控件(菊花)
    private lazy var activity:UIActivityIndicatorView = {
        
        //设置成大白的样式
        let activity = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
        
    
        activity.color = UIColor.redColor()
        
        return activity
    }()
    
    
    //懒加载下拉刷新的控件
    private lazy var refreshCon:HZRefrashControl = {
        
       let refreshCon = HZRefrashControl()
        
        refreshCon.frame.size.width = screenWidth
        refreshCon.frame.size.height = 50
        
        return refreshCon
    }()
    
    
    //懒加载提示的标栏
    private lazy var reminderLabel:UILabel = {
        
        let reminderLabel = UILabel()
        
        reminderLabel.backgroundColor = UIColor.orangeColor()
        
        reminderLabel.textColor = UIColor.whiteColor()
        
        reminderLabel.font = UIFont.systemFontOfSize(14)

        //设置文字区中
        reminderLabel.textAlignment = .Center
        
        //设置控件的大小
        reminderLabel.frame.size = CGSize(width: screenWidth, height: 35)
        
        
        //默认隐藏
        reminderLabel.hidden = true
        
        return reminderLabel
    }()
    
    
}


extension HZHomeTc {
    
    
    // MARK: - Table view data source
    
    //当每个cell将要显示的时候
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        
        //如果是最后一个 cell,并且菊花没有转
        if indexPath.row == (homeVM.arrM?.count)!-1 && !activity.isAnimating(){
            
            print("菊花转")
            
            //显示菊花
            activity.hidesWhenStopped = false
            
            //菊花开始转
            activity.startAnimating()
            
            //获取网络数据
            giveHome()

            
        }
        
    }
    
    
    
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return  self.homeVM.arrM?.count ?? 0
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //取出 cell
        let cell = tableView.dequeueReusableCellWithIdentifier("homecell", forIndexPath: indexPath) as! HZHomeCell
        
        
        
        //取出数据
        let cellVM = homeVM.arrM![indexPath.row]
        
        
        
        
        cell.cellVM = cellVM
        
        return cell
    }

    
}
